package com.common.boot.service.impl;

import com.common.boot.service.ImageService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class ImageServiceImpl implements ImageService {

    @Value("${application.img}")
    private String path;
    @Override
    public String ngUploadImg(MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        String uuid = UUID.randomUUID().toString();
        File file = new File(path);
        String orgFileName = multipartFile.getOriginalFilename();
        String substring = orgFileName.substring(orgFileName.lastIndexOf('.'));
        if(!file.exists()){
            file.mkdirs();
        }
        String url = path+uuid+substring;
        multipartFile.transferTo(new File(url));
        return url;
    }
}
